Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Getting wrong width value [2px extra] from getBoundingClientRect()

This is being weird. I am using getBoundingClientRect() to get the values to position my blue coloured div so that it can surround an element on mouseover.

It works great and is getting all the values correctly. See this for reference:

enter image description here

As you can see, the blue div here surrounded the password element on hover as it should! (I have used Bootstrap demo form from w3schools for this.)

However, the problem arises when I create a form page myself without using Bootstrap or any other framework.

It gets the top and left correctly, but width and height 2px more! See the extra spacing here:

enter image description here

I don't know what is causing this to output those 2px extra. Why it works perfectly with Bootstrap or W3.CSS like frameworks but spit out 2px more without any frameworks? What's the workaround for this?

Here's the code snippet:

document.addEventListener("mouseover", (e) => {
  elem = e.target;
  var rect = elem.getBoundingClientRect();

  var x = rect.left;
  var y = rect.top;
  var w = rect.width;
  var h = rect.height;

  div.style.width = w + "px";
  div.style.height = h + "px";
  div.style.left = x + window.scrollX + "px";
  div.style.top = y + window.scrollY + "px";
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!